<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');

$ops = array('door_teacher','door_teacher_button','door_student','door_student_button');
$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : '';

global $_W;
$rolearray = array('admin','teacher','student');
$rolenamearray =array(
    'admin'=>'管理员',
    'teacher'=>'教师',
    'student'=>'课代表'
);

if($op =='door_teacher'){
    $role = $rolearray[0];
    //以防万一，先判断role是否为admin
    $openid = $_W['openid'];
    $current_admin = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_admin['role'] != $role){
        $prompt = '您的角色不是管理员，无法使用解绑功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    //得到该管理员管理的柜子id
    $current_cabinet = array();
    $all = pdo_getall('homework_cabinet_admin', array('uniacid'=>$_W['uniacid']), array('userid','device_info_id','device_name'));
    foreach($all as $key=>$value){
        $temp = explode(',',$value['userid']);
        if(in_array($current_admin['id'] , $temp))
            $current_cabinet[] = array(
                'info_id' => $value['device_info_id'],
                'device_name' => $value['device_name']
            );
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前管理员并没有绑定任何柜子！';
        include wl_template('tips/prompt');
        exit;         
    }
    //根据柜子数组，判断柜子的柜门的userid是否为空，获得door和name
    $current_door = array();
    foreach($current_cabinet as $key=>$value){
        $all = pdo_getall('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id']), array('userid','door'));
        foreach($all as $k=>$val){//确定下来了柜子和柜门
            //userid可能为空，或一个教师
            if($val['userid']!=''){
                $teacher_name = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'id'=>$val['userid']), array('name'));
                $current_door[] = array(
                    'info_id' => $value['info_id'],
                    'device_name' => $value['device_name'],
                    'door' => $val['door'],
                    'userid' => $val['userid'],//其实这项没有用
                    'name' => $teacher_name['name']
                );
            }
        }
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_door)==0){
        $prompt = '当前管理员绑定的柜子并没有绑定教师！';
        include wl_template('tips/prompt');
        exit;         
    }
    // print_r($current_door);
	include wl_template('unbinddown/door_teacher');
    exit;
}
if($op =='door_teacher_button'){//按钮后只是返回提示信息
    $arr = $_POST['arr'];
    $data = '您已经成功解绑！';
    if(count($arr)==0){
        print_r('您还未选择多选框');
        exit;
    } 
    foreach($arr as $key=>$value){
        $array = array('userid'=>'');
        pdo_update('homework_door_teacher' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door' => $value['door']));
        //老师解绑后，还需要对同一个柜门和学生们解绑
        pdo_update('homework_door_student' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door' => $value['door']));
        $data .= $value['device_name'].'（ID为'.$value['info_id'].'）'.$value['door'].'号门：'.$value['name'];
        $data .='———';
    }
    $data = substr($data,0,strlen($str)-9);
    print_r($data);
}
if($op =='door_student'){
    $role = $rolearray[1];
    //以防万一，先判断role是否为teacher
    $openid = $_W['openid'];
    $current_teacher = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_teacher['role'] != $role){
        $prompt = '您的角色不是教师，无法使用解绑功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    //得到该教师管理的柜门信息，注意每个柜门只有一个教师
    $id_to_name = $this->get_id_to_name();
    $teacher_id = $current_teacher['id'];
    $current_door_teacher = array();
    $all = pdo_getall('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'userid'=>$teacher_id), array('device_info_id','door'));
    foreach($all as $key=>$value){
        $current_door_teacher[] = array(
            'info_id' => $value['device_info_id'],
            'device_name' => $id_to_name[$value['device_info_id']],
            'door' => $value['door']
        );        
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_door_teacher)==0){
        $prompt = '当前教师并没有绑定任何柜门！';
        include wl_template('tips/prompt');
        exit;         
    } 
    
    //根据柜门数组，判断柜门的userid是否为空，获得name
    $current_door_student = array();
    foreach($current_door_teacher as $key=>$value){
        $temp_student = pdo_get('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid')); 
        if($temp_student['userid']!=''){//非空说明该柜门有绑定的学生
            $temp_student_s = explode(',' , $temp_student['userid']);
            foreach($temp_student_s as $k=>$val){
                $name = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'id'=>$val), array('name')); 
                 $current_door_student[] = array(
                    'info_id' => $value['info_id'],
                    'device_name' => $value['device_name'],
                    'door' => $value['door'],
                    'userid' => $val,
                    'name' => $name['name']               
                 );
            }
        }      
    }
    
    //检查该数组，如果为空，则提示信息
    if(count($current_door_student)==0){
        $prompt = '当前教师绑定的柜门并没有任何课代表绑定！';
        include wl_template('tips/prompt');
        exit;         
    }    
    
     //print_r($current_door_student);
	include wl_template('unbinddown/door_student');
    exit;
}
if($op =='door_student_button'){//按钮后只是返回提示信息
    $arr = $_POST['arr'];
    $data = '您已经成功解绑！';
    if(count($arr)==0){
        print_r('您还未选择多选框');
        exit;
    } 
    foreach($arr as $key=>$value){
        $temp_userid = pdo_get('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid')); 
        $temp_userid_s = explode(',' , $temp_userid['userid']);
        foreach($temp_userid_s as $k=>$val){
            if($val==$value['userid']){
                unset($temp_userid_s[$k]);
                break;
            }
        }
       
        $to_userid = implode(',' , $temp_userid_s);
  
        $array = array('userid'=>$to_userid);
        pdo_update('homework_door_student' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door' => $value['door']));
        $data .= $value['device_name'].'（ID为'.$value['info_id'].'）'.$value['door'].'号门：'.$value['name'];
        $data .='———';
    }
    $data = substr($data,0,strlen($str)-9);
    print_r($data);
}